package demo2String;

import org.junit.Test;

/**
 * @ClassName $ {NAME}
 * @Description TODO
 * 请实现一个函数，将一个字符串中的每个空格替换成“%20”。例如，当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
 * @Author shenxinyuan
 * @Date $ {DATE} $ {TIME}
 * @Version 1. 0
 **/

public class StringSpaceReplacement {

    String str = "We Are Happy.";

    public String replaceSpace (String s) {
        // write code here
        char[] chars = s.toCharArray();
        String res = "";
        for(int i=0;i<chars.length;i++){
            if(chars[i]==' '){
                res += "%20";
            }else {
                res += chars[i];
            }
        }
        return res;
    }

    /*
    public String replaceSpace (String s) {
        if (s == null || "".equals(s))
            return s;
        return s.replaceAll(" ", "%20");
    }
*/
    @Test
    public void test() {

        System.out.println("replaceSpace(str) = " + replaceSpace(str));

    }
}
